{extend name="common/__base" /}
{block name="body"}
<table id="table" lay-filter="table"></table>

<!-- 操作栏start -->
<script id="operation" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 操作栏end -->

<!-- 工具栏模板start -->
<script id="toolbar" type="text/html">
    <button class="layui-btn layui-btn-sm" lay-event="add" title="添加管理员"><i class="layui-icon">&#xe654;</i></button>
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="refresh"><i class="layui-icon">&#xe9aa;</i></button>
</script>
<!-- 工具栏模板end -->
{/block}

{block name="js"}
<script>
    var form = layui.form;
    var table = layui.table;

    // 数据表格
    var dataTable = table.render({
        elem: "#table",
        cols: [[
            {field: "id", title: "ID", sort: true},
            {field: "title", title: "角色名"},
            {field: "create_time", title: "时间", sort: true},
            {title: "操作", toolbar: "#operation"}
        ]],
        url: "{:url('admin/admin/roleList')}",
        method: "post",
        parseData: function (ret) {
            return {
                code: ret.code,
                msg: ret.msg,
                count: ret.data.count,
                data: ret.data.data
            };
        },
        toolbar: "#toolbar",
        page: true
    });

    // 工具操作--编辑、删除
    table.on("tool(table)", function (obj) {
        if (obj.event == "del") {
            layer.confirm("确定删除吗？", {icon: 3, title: "删除角色"}, function () {
                $.ajax({
                    url: "{:url('admin/admin/delRole')}",
                    type: "post",
                    data: {id: obj.data.id},
                    dataType: "json",
                    success: function (ret) {
                        layer.msg(ret.msg);
                        if (ret.code == 1) {
                            location.reload();
                        }
                    }
                });
            });
        } else if (obj.event == "edit") {
            layer.open({
                type: 2,
                title: "编辑角色",
                shade: 0.6,
                shadeClose: true,
                maxmin: true,
                area: ["500px", "600px"],
                content: "{:url('admin/admin/editRole')}?role_id=" + obj.data.id
            });
        }
    });

    // 工具栏--添加、刷新
    table.on("toolbar(table)", function (obj) {
        if (obj.event == "add") {
            layer.open({
                type: 2,
                title: "添加角色",
                shade: 0.6,
                shadeClose: true,
                maxmin: true,
                area: ["500px", "600px"],
                scrollbar: true,
                content: "{:url('admin/admin/addRole')}"
            });
        } else if (obj.event == "refresh") {
            location.reload();
        }
    });
</script>
{/block}